<?php
class ImgsController extends AppController {

	var $name = 'Imgs';
	var $components = array('Uploader.Uploader');
	function beforeFilter() {
        parent::beforeFilter();
		$this->Uploader->uploadDir = 'files/uploads/gallery/'; //thu muc chua file upload
        $this->Uploader->enableUpload = true;
        $this->Uploader->maxFileSize = '5M'; // quy dinh dung luong duoc upload len toi da la 2 Megabytes
        $this->Uploader->maxNameLength = 25;//do dai cua ten file
        $this->Uploader->tempDir = TMP;
        
    }
	function admin_index($gid = NULL) {
		$this->Img->recursive = 0;
   		$conditions = array('Img.galleries_id' => $gid);
 		$this->paginate = array(
			'conditions'=>$conditions,
			'order'=> 'Img.id ASC'
		);
 		$this->set('imgs', $this->paginate());
		$galleries = $this->Img->Gallery->find('list');
		$choose = $this->Img->Gallery->find('first',array('conditions'=>array('Gallery.id'=>$gid)));
  		$this->set(compact('galleries','gid','choose'));
	}
	function admin_view($gid = null) {		
		$this->Img->recursive = 1;		
		$conditions = array('Img.galleries_id' => $gid);
 		$this->paginate = array(
			'conditions'=>$conditions,
			'order'=> 'Img.id ASC'
		); 	
		$this->set('images', $this->paginate());
		$galleries = $this->Img->Gallery->find('list');
		$choose = $this->Img->Gallery->find('first',array('conditions'=>array('Gallery.id'=>$gid)));
  		$this->set(compact('galleries','gid','choose'));		
	}
 	 function admin_add() {
			//debug($id);			
			$this->Img->recursive = 1;
			$galleries = $this->Gallery->find('list',array('order'=>'Gallery.id DESC'));
			$this->set(compact('galleries'));			
			//debug($this->data['Img']['galleries_id']);
			$data = array();
			//debug($id);
			//$data['Img']['galleries_id'] = $id;
			//debug($data);
			if(!empty($this->data)){
				//debug($id);
				$data['Img']['galleries_id'] = $this->data['Img']['galleries_id'];
				$upload = $this->Uploader->uploadAll(array('url1','url2','url3','url4','url5'));					
				for($i=1;$i<6;$i++){					
					if(isset($upload['url'.$i])){
						$data['Img']['url'] =  $upload['url'.$i]['name'];								
						$data['Img']['note'] = $this->data['Img']['note'.$i];
						//debug($data);
						$this->Img->create();
						if ($this->Img->save($data)){								
							$this->Session->setFlash(__('Đã lưu.', true));					
							//debug($data);
						} else {
							$this->Session->setFlash(__('Có lỗi xảy ra. Hãy thử lại!', true));
						}						
					}					
				}			
				$this->redirect(array('controller' =>'imgs','action' => 'view',$data['Img']['galleries_id']));
				
			}
			//debug($data['Img']['galleries_id']);
		}
	function _delimg($id = null){
			$files = $this->Img->find('list',array(
				'conditions'=>array('Img.id'=>$id),
				'fields'=>'url')
			);
			$file ='';
			$file = "files" . DS . "uploads" . DS. "gallery" . DS .$files[$id];
			if(is_file($file)){
				unlink($file);
			}
	}
	function admin_delete($gid=NULL, $id = NULL) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại ảnh này !', true));
			$this->redirect(array('action'=>'index',$gid));
		}
		$this->_delimg($id);
		if ($this->Img->delete($id)) {
			$this->Session->setFlash(__('Đã xóa !', true));
			$this->redirect(array('action'=>'index',$gid));
		}
		else{
		$this->Session->setFlash(__('Có lỗi sảy ra. Xóa không thành công !', true));
		$this->redirect(array('action' => 'index',$gid));
		}
	}
	function admin_mutildelete($str = null){
        if($str){
            $arrid=explode(',',$str);
        }else{
    		$this->Session->setFlash(__('Có lỗi sảy ra. Xóa không thành công !', true));
    		$this->redirect(array('action' => 'index',$str));
        }
         foreach($arrid as $item){
			$this->_delimg($item);
    		$this->Img->delete($item);
         }
		$this->Session->setFlash(__('Đã xóa !', true));
		$this->redirect(array('action' => 'index',$str));
    }

    function index($id = null) {
		$gid = $this->data['Img']['galleries_id'];
		if (!$id && !$gid) {
			$this->Session->setFlash(__('Bạn phải chon 1 thư viện để xem ảnh !', true));
			$this->redirect(array('controller'=>'galleries','action' => 'index'));
		}

		if($gid != null){
			$condition = array('Img.galleries_id'=>$gid);
		}elseif($id){
			$condition = array('Img.galleries_id'=>$id);
		}
		$this->paginate = array(
				'conditions'=>$condition
			);

		$this->set('images', $this->paginate());
		$galleries = $this->Img->Galleries->find('list');
		if(!$id){
			$id = $gid;
		}
		$choose = $this->Img->Galleries->find('first',array('conditions'=>array('id'=>$id)));
		$this->set(compact('galleries','id','choose'));
	}
}